var MapIconMaker = {};

Array.prototype.clearMarkers = function() {
    if(this.length > 0) {
        for(var i=0; i < this.length; i++)
        {
            this[i].setMap(null);
        }
        this.length = 0;
    }
    return this;
}

MapIconMaker.addMarkerToMap = function(map, color, title, position){
    return new MarkerWithLabel({
        map: map,
        draggable:false,
        animation: google.maps.Animation.DROP,
        icon: createIcon(color),
        position: position,
        title: title/*,
        labelContent: title,
        labelAnchor: new google.maps.Point(22, -5),
        labelClass: "labels", // the CSS class for the label
        labelStyle: {opacity: 1.0}*/
    });
}

MapIconMaker.createLabeledMarkerIcon = function (opts) {
    var primaryColor = opts.primaryColor || "#DA7187";
    var strokeColor = opts.strokeColor || "#333333";
    var starPrimaryColor = opts.starPrimaryColor || "#FFFF00";
    var starStrokeColor = opts.starStrokeColor || "#0000FF";
    var label = MapIconMaker.escapeUserText_(opts.label) || "";
    var labelColor = opts.labelColor || "#000000";
    var addStar = opts.addStar || false;

    var pinProgram = (addStar) ? "pin_star" : "pin";
    var baseUrl = "http://chart.apis.google.com/chart?cht=d&chdp=mapsapi&chl=";
    var iconUrl = baseUrl + pinProgram + "'i\\" + "'[" + label + 
        "'-2'f\\"  + "hv'a\\]" + "h\\]o\\" + 
        primaryColor.replace("#", "")  + "'fC\\" + 
        labelColor.replace("#", "")  + "'tC\\" + 
        strokeColor.replace("#", "")  + "'eC\\";
        if (addStar) {
        iconUrl += starPrimaryColor.replace("#", "") + "'1C\\" + 
        starStrokeColor.replace("#", "") + "'0C\\";
    } iconUrl += "Lauto'f\\";

    var icon = {}
        icon.image = iconUrl + "&ext=.png";
    return icon.image;
};

MapIconMaker.escapeUserText_ = function (text) {
  if (text === undefined) {
    return null;
  }
  text = text.replace(/@/, "@@");
  text = text.replace(/\\/, "@\\");
  text = text.replace(/'/, "@'");
  text = text.replace(/\[/, "@[");
  text = text.replace(/\]/, "@]");
  return encodeURIComponent(text);
};
